package cn.zzp.ggkt.vod.controller;


import cn.zzp.ggkt.model.vod.Teacher;
import cn.zzp.ggkt.result.Result;
import cn.zzp.ggkt.vo.vod.TeacherQueryVo;
import cn.zzp.ggkt.vod.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台管理讲师 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2022-08-21
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/vod/teacher")
@Api(tags = "讲师管理接口")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有讲师
     * @return
     */
    @ApiOperation("查询所有讲师")
    @GetMapping
    public Result findAll() {
            List<Teacher> list = teacherService.list();
            return Result.ok(list);
    }

    @ApiOperation("根据id查询讲师")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id) {
        Teacher teacher = teacherService.getById(id);
        return Result.ok(teacher);
    }

    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("/{id}")
    public Result removeById(@ApiParam(name = "id", value = "ID", required = true)
                                 @PathVariable("id") Long id) {

        boolean b = teacherService.removeById(id);
        if (b) {
            return Result.ok(null);
        }

        return Result.fail(null);

    }

    /**
     * 条件查询分页列表
     * @param queryVo
     * @return
     */
    @ApiOperation("获取分页列表")
    @PostMapping
    public Result pageQuery(@RequestBody TeacherQueryVo queryVo){


            IPage<Teacher> teacherPage = teacherService.pageQuery(queryVo);
            return Result.ok(teacherPage);

    }

    /**
     * 添加或者更新讲师
     * @param teacher
     * @return
     */
    @ApiOperation("添加讲师接口")
    @PutMapping
    public Result save(@RequestBody Teacher teacher){

        boolean b = teacherService.saveOrUpdate(teacher);
        if (b) {
            return Result.ok(null);
        }

        return Result.fail(null);
    }

    /**
     * 批量操作
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据id列表删除")
    @PatchMapping
    public Result batchDel(@RequestBody List<Long> ids) {

        boolean b = teacherService.removeByIds(ids);
        if (b) {
            return Result.ok(null);
        }

        return Result.fail(null);
    }
}

